import {Injectable} from "@angular/core";
import {HttpService} from "../HttpService";
import {Menu} from "../model/Menu";
import {ResponseData} from "../model/ResponseData";
import {Http} from "@angular/http";
import {ArrayModel} from "../model/ArrayModel";


/**
 * 获得菜单信息
 */
@Injectable()
export class MenusService extends HttpService{

    // Func {Create};

    public _customMenus:ArrayModel<Menu>=null;


    /**
     * 获得用户菜单
     * @type {string}
     */
    protected url:string="GetCustomMenus";

    protected allMenuUrl:string="AllMenus";

    protected createMenuUrl:string="CreateMenu";

    constructor(private http:Http){super()};


    public getCustomMenus():Promise<ResponseData<Menu[]>>{
        return this.http.get(this.prefix + this.url)
            .map(response => {
                return  response.json() as ResponseData<Menu[]>;
            }).toPromise();
    }


    public getAllMenus():Promise<ResponseData<Menu[]>>{

        return this.http.get(this.prefix + this.allMenuUrl)
            .map(response => {
                return  response.json() as ResponseData<Menu[]>;
            }).toPromise();
    }


    public createMenu(menu:Menu):Promise<ResponseData<number>>{

        return this.http.put(this.prefix + this.createMenuUrl,
        menu)
            .map(response => {
                return  response.json() as ResponseData<number>;
            }).toPromise();
    }


    deleteMenuUrl="DeleteMenu";

    public deleteMenu(ID:number):Promise<ResponseData<boolean>>{

        return this.http.delete(this.prefix + this.deleteMenuUrl + "?" + "ID="+ID)
            .map(response => {
                return  response.json() as ResponseData<boolean>;
            }).toPromise();
    }


    updateMenuUrl="UpdateMenu";

    public updateMenu(menu:Menu):Promise<ResponseData<boolean>>{
        return this.http.post(this.prefix + this.updateMenuUrl,menu)
            .map(response => {
                return  response.json() as ResponseData<boolean>;
            }).toPromise();
    }

    sortMenuUrl="SortMenu";

    public sortMenu(menu:any,sortType:string):Promise<ResponseData<boolean>>{
        menu.sortType=sortType;
        return this.http.post(this.prefix + this.sortMenuUrl,{"ID":menu.ID,"sortType":sortType})
            .map(response => {
                return  response.json() as ResponseData<boolean>;
            }).toPromise();
    }




}
